import 'package:flutter/material.dart';
import 'package:signals_flutter/signals_flutter.dart';
import '../application/article_view_model.dart';
import '../../../../di/service_locator.dart';

class ArticleListPage extends StatelessWidget {
  const ArticleListPage({super.key});

  @override
  Widget build(BuildContext context) {
    final vm = getIt<ArticleViewModel>();

    WidgetsBinding.instance.addPostFrameCallback((_) => vm.load());

    return Scaffold(
      appBar: AppBar(title: const Text('Articles')),
      body: Watch.builder(
        builder: (context) {
          final async = vm.articles.value;
          if (async.isLoading) {
            return const Center(child: CircularProgressIndicator());
          } else if (async.hasError) {
            return Center(child: Text(async.error.toString()));
          } else {
            final list = async.value ?? [];
            return ListView.builder(
              itemCount: list.length,
              itemBuilder: (_, i) => ListTile(title: Text(list[i].title)),
            );
          }
        },
      ),
    );
  }
}